import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../compoents/Welcome.vue'

Vue.use(VueRouter)

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            { path: '/welcome', component: Welcome },
        ]
    },

]


const router = new VueRouter({
        mode: 'hash',
        routes
    })
    //添加路由导航守卫 
router.beforeEach((to, from, next) => {
    //如果访问的地址是login那就放行
    if (to.path === '/login') return next()
    const tokenStr = window.localStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})


export default router